package com.cj.bk_app.ui.book

import androidx.compose.foundation.lazy.LazyListState
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.cj.bk_app.base.BaseViewModel
import com.cj.bk_app.model.bean.BKResponsePage
import com.cj.bk_app.model.bean.Book
import com.cj.bk_app.model.bean.Result
import com.cj.bk_app.model.repository.BookRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
open class BookViewModel @Inject constructor() : BaseViewModel() {

    @Inject
    lateinit var homeRepository: BookRepository


    sealed class ArticleType {
        object Home : ArticleType()                 // 首页                // 搜索
    }

    private val _uiState = MutableLiveData<ArticleUiModel>()
    val uiState: LiveData<ArticleUiModel>
        get() = _uiState

    private var currentPage = 0

    val lazyListState = LazyListState()
    private val allArticleList = arrayListOf<Book>()


    fun getHomeArticleList(isRefresh: Boolean = false) = getArticleList(ArticleType.Home, isRefresh)

    private fun getArticleList(
        articleType: ArticleType,
        isRefresh: Boolean = false,
        cid: Int = 0,
        key: String = ""
    ) {
        viewModelScope.launch(Dispatchers.Main) {
            emitArticleUiState(isRefresh, showSuccess = allArticleList, showEnd = true)
            if (isRefresh) {
                allArticleList.clear()
                currentPage = 1
            }

            val result: Result<BKResponsePage<Book>> = homeRepository.getArticleList(currentPage)

            if (result is Result.Success) {
                val articleList = result.data
                if (articleList.current >= articleList.total || articleList.records.size == articleList.total) {
                    emitArticleUiState(
                        showLoading = false,
                        showEnd = true,
                        showSuccess = allArticleList.apply { addAll(articleList.records) })
                    return@launch
                }
                currentPage++
                emitArticleUiState(
                    showLoading = false,
                    showSuccess = allArticleList.apply { addAll(articleList.records) },
                    isRefresh = isRefresh
                )
            } else if (result is Result.Error) {
                emitArticleUiState(showLoading = false, showError = result.exception.message)
            }
        }
    }

    private fun emitArticleUiState(
        showLoading: Boolean = false,
        showError: String? = null,
        showSuccess: List<Book>? = null,
        showEnd: Boolean = false,
        isRefresh: Boolean = false,
        needLogin: Boolean? = null
    ) {
        val uiModel =
            ArticleUiModel(showLoading, showError, showSuccess, showEnd, isRefresh, needLogin)
        _uiState.value = uiModel
    }


    data class ArticleUiModel(
        val showLoading: Boolean,
        val showError: String?,
        val showSuccess: List<Book>?,
        val showEnd: Boolean, // 加载更多
        val isRefresh: Boolean, // 刷新
        val needLogin: Boolean? = null,
        val listState: LazyListState = LazyListState()
    )


}